package keter.framework.util;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang3.RandomStringUtils;

/**
 * 随机测试数据生成工具类.
 * 
 * @author calvin
 */
public class RandomUtil {

	private static Random random = new Random();
	private static SecureRandom srand = new SecureRandom();
	
	/**
	 * 返回随机ID.
	 */
	public static long srandomId() {
		return srand.nextLong();
	}

	/**
	 * 返回随机ID.
	 */
	public static long randomId() {
		return random.nextLong();
	}

	/**
	 * 返回随机名称, prefix字符串+5位随机数字.
	 */
	public static String randomName(String prefix) {
		return prefix + random.nextInt(10000);
	}
	
	/**
	 * 随机参数长度的大小写字母
	 */
	public static String randomString(int length) {
	    return RandomStringUtils.randomAlphabetic(length);
	}

	/**
	 * 从输入list中随机返回一个对象.
	 */
	public static <T> T randomOne(List<T> list) {
		Collections.shuffle(list);
		return list.get(0);
	}

	/**
	 * 从输入list中随机返回n个对象.
	 */
	public static <T> List<T> randomSome(List<T> list, int n) {
		Collections.shuffle(list);
		return list.subList(0, n);
	}

	/**
	 * 从输入list中随机返回随机个对象.
	 */
	public static <T> List<T> randomSome(List<T> list) {
		int size = random.nextInt(list.size());
		if (size == 0) {
			size = 1;
		}
		return randomSome(list, size);
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			System.out.println(RandomUtil.srandomId());
		}
		for (int i = 0; i < 10; i++) {
			System.out.println(RandomUtil.randomId());		}
	}
}
